package org.joy;

import com.oracle.bmc.Region;
import com.oracle.bmc.auth.AuthenticationDetailsProvider;
import com.oracle.bmc.auth.SimpleAuthenticationDetailsProvider;
import com.oracle.bmc.objectstorage.ObjectStorageClient;
import com.oracle.bmc.objectstorage.requests.GetNamespaceRequest;
import com.oracle.bmc.objectstorage.requests.PutObjectRequest;
import com.oracle.bmc.objectstorage.responses.PutObjectResponse;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.DoubleStream;

public class OssClient {
    private static  String tenancyid = "=ocid1.tenancy.oc1..aaaaaaaacfchxfdz6qhnnmlmxep7qpzj36q4ceojog4pvkct7woxillugaca";
    private static  String userid = "ocid1.user.oc1..aaaaaaaamh336a3sicdnq2zopjynhbgmnyfy54r5eztllrm6vv6kwcbe52sa";
    private static  String fingerprint = "ce:c6:a6:24:45:72:9c:92:f1:b6:59:53:81:2d:0a:c8";
    private static  String rsa_priv_key="-----BEGIN PRIVATE KEY-----\n" +
            "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDMKlv6rLxRlqbM\n" +
            "ZW86QC+BL/KB1eR2cmYls5koIICnbPqWUa78L6XgOfS061OO/M/HHTynIhmC6ifp\n" +
            "Q7zyzDoL9/vaKmsyrwkctlES8yems6/cGBNfUrI1OEgH0d8IcOQ7kagC7hZA5mht\n" +
            "lFZnoE8oBf/BWV5whIoMmXVfACUUuVrhOEhD2O3fl7uUh2vjrkWHuRT5xZE/Lag2\n" +
            "ctXS8QaI+IIvMuGmjgHc0kx/k2c8ODf+/HUoMII6AaaupwVwpYkIsCm4R/qy0bv1\n" +
            "s/e6+f1yGb1HhaEid6HyOShPhhxRt01WtsE+XFRgnN4qjTqjRYWPT407l9+meB88\n" +
            "jDMiSvvvAgMBAAECggEAHj5aILtCLEMzqIyQGky+x2rUXXz+BQ5zk5B+wW3LJmy4\n" +
            "1C1RIroQ3t3cTN6dTU2lnnp2F9Cv1G+eWOoit8QesdOFgy8McStySfFh93t95gOx\n" +
            "Ky/afXY7wDF6+MQ96eYF5nhwWdAYtiV2K7axHqml1X50q+4dVMVKyTdZpn3YKwZP\n" +
            "Amf2hmJPTtWHeC9Tl1RpRZkTLMJ9P9wAV4lu7xTuoDBAzqvpVxJtphKgJjTt30ef\n" +
            "gzOo3aP1fkdLkBOablQKCa6dU7ilVZORS7Ei51/xkOXtvdjWFPEeA/8kxEpPeQjl\n" +
            "aG9sq0aahuZLSmofFUaEGxDeL4+yb3/Nm4aQ9mf4QQKBgQDrfVq9r0rOFPEwpMdV\n" +
            "IzzfGgWStkhu/4bfk4ygt79D+HwGlusWyBwdAdS3zKquBOwOfHewFAukXrXNsExt\n" +
            "9WbnrObbsP3Ca4BIY+rajnVKELLEbjleuiuhbSx0MXU9ocnr0TWTjbF/DEtC4JxO\n" +
            "/leYyuiCeRF2B6fPbySJLP27QQKBgQDd8pQAu6yjMjHktd2jMRx4SyygM6rJvARR\n" +
            "7IrKFKLjgo2bBCKKb3Po6XOPqGzUFwDz75icc++fe2MTGQ2VacjOTvL7TF/dBnlA\n" +
            "OJsGuE2QQ8oUDg+x33pEvlixlVQ19PmZsE6KsvMrkia7xUc4JAGMBQET2JYVwFB4\n" +
            "JdyFWEXbLwKBgEqMfYxtEMZatNwMXhoitKOALmF6mUWiWQUwRaWfZcpTwJepcTTe\n" +
            "FqBMMxYYkbPTVRNk7dLtFZ5saSt7c0agGHqhLtg/mKXSuGKyVufnj1PqI12x8zF8\n" +
            "yFErKZaDDbV8IVso+MgEZDtvHehHCNStCfgRzDd5skBmPgaaDbvrZUvBAoGAb8cx\n" +
            "vsTGB1LKSfNF8mBThEnn6a7bvsnQKypEI84M0yP3CTEl7C6BY7XfG2Spmo8TTcdH\n" +
            "RtQkxF8x4HZnnu4t0al1OhaW2QvfmPZpLRM5DQT3Cu6h/OUeAO0UYitgw+PiqQJg\n" +
            "K/4/9ZufokWSvrVzif8jMcmMnNvEKx52VznDxMUCgYEAvTd6JeiSla20r9OB5ZXk\n" +
            "wSj93MBWxFnuiR4wurX7sUvy8nvzzaIQ2QMx/0GFkB9K6nQM/Dqq+7wb1nnR2HOI\n" +
            "IgN7qf796eAPMBe6ED9nVZ2xuLJJ/V7vZ4Mw6JytHmdXJLC2dX5dz9WlF47t7JaL\n" +
            "4j4MoqSSi3og40GDkzZB29M=\n" +
            "-----END PRIVATE KEY-----";
//    private static  String  apiKeyId =String.format("%s/%s/%s",tenancyid, userid, fingerprint);

    public static void main(String[] args) throws UnsupportedEncodingException {
        final AuthenticationDetailsProvider provider = SimpleAuthenticationDetailsProvider.builder()
                .fingerprint(fingerprint)
                .privateKeySupplier( () -> {
                    try {
                        return new ByteArrayInputStream(rsa_priv_key.getBytes("UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        throw new RuntimeException(e);
                    }
                })
                .tenantId(tenancyid)
                .userId(userid).build();
        ObjectStorageClient objectStorageClient =
                ObjectStorageClient.builder().region(Region.US_PHOENIX_1).build(provider);
        putObject(objectStorageClient);
    }

    public static void putObject(ObjectStorageClient client){
        String bucketname="buckettest";
        String objectname="demo3";
        String str = "String与InputStream相互转换";
        String ns ="cnjxmruwj0jf";
        InputStream out   =   new ByteArrayInputStream(str.getBytes());
        Map<String ,String > objmeta = new HashMap<>();
        objmeta.put("name","joy");
        PutObjectRequest putObjectRequest = PutObjectRequest.builder()
                .namespaceName(ns)
                .bucketName(bucketname)
                .objectName(objectname)
                .putObjectBody(out)
                .opcMeta(objmeta)
                .build();
        PutObjectResponse putObjectResponse = client.putObject(putObjectRequest);
        System.out.println(putObjectResponse.getHeaders());
    }



    public static void bulkputObject(ObjectStorageClient client){
        String bucketname="buckettest";
        String objectname="demo3";
        String str = "String与InputStream相互转换";
        String ns ="cnjxmruwj0jf";
        InputStream out   =   new ByteArrayInputStream(str.getBytes());
        Map<String ,String > objmeta = new HashMap<>();
        objmeta.put("name","joy");

        PutObjectRequest putObjectRequest = PutObjectRequest.builder()
                .namespaceName(ns)
                .bucketName(bucketname)
                .objectName(objectname)
                .putObjectBody(out)
                .opcMeta(objmeta)
                .build();
        PutObjectResponse putObjectResponse = client.putObject(putObjectRequest);
        System.out.println(putObjectResponse.getHeaders());
    }

    public static void getNS(ObjectStorageClient client) {


        // Construct GetNamespaceRequest with the given compartmentId.
        GetNamespaceRequest getNamespaceRequest =
                GetNamespaceRequest.builder().compartmentId(tenancyid).build();
        String namespace = client.getNamespace(getNamespaceRequest).getValue();

        System.out.println(
                String.format(
                        "Object Storage namespace for compartment [%s] is [%s]",
                        tenancyid, namespace));

    }

}
